<template>
  <div class="ma-content-block">
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :span="8">
          <a-form-item label="菜单名称" field="menu_name">
            <a-input v-model="searchForm.menu_name" placeholder="请输入菜单名称" />
          </a-form-item>
        </a-col>
      </template>

      <!-- Table 自定义渲染 -->
      <template #icon="{ record }">
        <div class="icon12__item" :style="{ backgroundColor: record.color, color: '#FFFFFF' }">
          <div
            style="text-shadow: rgba(0, 0, 0, 0.16) 2px 4px 5px; transform: rotate(19deg)"
            :class="[record.icon]"></div>
        </div>
      </template>
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import commonApi from '@/api/common'
import EditForm from './edit.vue'
import api from '../../api/app/menu'

// 引用定义
const crudRef = ref()
const editRef = ref()
const optionData = reactive({
  treeData: [],
})

// 搜索表单
const searchForm = ref({
  menu_name: '',
})

// SaTable 基础配置
const options = reactive({
  api: api.getPageList,
  rowSelection: { showCheckedAll: true },
  isExpand: true,
  add: {
    show: true,
    auth: ['/app/saiuser/app/AppMenu/save'],
    func: async () => {
      editRef.value?.open()
    },
  },
  edit: {
    show: true,
    auth: ['/app/saiuser/app/AppMenu/update'],
    func: async (record) => {
      editRef.value?.open('edit')
      editRef.value?.setFormData(record)
    },
  },
  delete: {
    show: true,
    auth: ['/app/saiuser/app/AppMenu/destroy'],
    func: async (params) => {
      const resp = await api.delete(params)
      if (resp.code === 200) {
        Message.success(`删除成功！`)
        crudRef.value?.refresh()
      }
    },
  },
})

// SaTable 列配置
const columns = reactive([
  { title: '菜单名称', dataIndex: 'menu_name', width: 180 },
  { title: '图标', dataIndex: 'icon', width: 120 },
  { title: '状态', dataIndex: 'status', type: 'dict', dict: 'data_status', width: 100 },
  { title: '排序', dataIndex: 'sort', width: 100 },
  { title: '是否链接', dataIndex: 'is_href', type: 'dict', dict: 'yes_or_no', width: 100 },
  { title: '页面地址', dataIndex: 'page_url', width: 180 },
])

// 页面数据初始化
const initPage = async () => {
  const resp = await api.getPageList()
  optionData.treeData = resp.data
}

// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}

// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>

<style>
@import '../../components/tn-icon/iconfont.css';
</style>

<style scoped lang="less">
.icon12__item {
  width: 42px;
  height: 42px;
  font-size: 26px;
  -webkit-transform: rotate(-19deg);
  transform: rotate(-19deg);
  border-radius: 43% 57% 43% 57% / 57% 43% 57% 43%;
  position: relative;
  z-index: 1;
}

.icon12__item::after {
  content: ' ';
  position: absolute;
  z-index: -1;
  width: 100%;
  height: 100%;
  left: 0;
  bottom: 0;
  border-radius: inherit;
  opacity: 1;
  -webkit-transform: scale(1) rotate(19deg);
  transform: scale(1) rotate(19deg);
  background-size: 100% 100%;
  background-image: url(@/views/saiuser/components/icon_bg4.png);
}
</style>
